﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Windows.Navigation;

using System.Collections.ObjectModel;
using System.ComponentModel;
using System.ComponentModel.DataAnnotations;
using System.Windows.Data;

using ProjManagSolution.pmMyWebService;
using ProjManagSolution.anMyWebService;
using System.Windows.Browser;

/*
 * MEMBER ROLE?
 * 
 */

namespace ProjManagSolution
{
    public partial class OwnedProject : Page
    {
        // Start Attribute
        protected pmWebServiceClient ws = new pmWebServiceClient();
        int projectID; //every project must have a project id
        string userEmail;
        protected user currUser;
        protected project currentProject = new project();

        protected List<user> memberList = new List<user>(); // team member list
        protected List<milestone> milestoneList = new List<milestone>();//milestone list
        protected List<documentation> documentList = new List<documentation>(); // documentation list

        protected Member selected = new Member(); //selected user on member list
        protected Milestone selected_milestone = new Milestone(); //selected milestone on 
        protected Phase selected_phase = new Phase(); //selected phase on phase list

        // Create a list to save all the Milestone for Getting PHASE
        // End Attributes

        public OwnedProject(int pid = 1)
        {
            //data initialization
            Session s = new Session();
            userEmail = s.GetUserEmail();
            projectID = pid;

            InitializeComponent();
            Loaded += new RoutedEventHandler(OwnedProject_Loaded);

            //on initialize, get the project info for Info tab
            ShowProjectInfo();

            ////get the current user information
            GetCurrentUserInfo();

            //get milestone
            GetMilestone();

            //get team member
            GetTeamMember();

            //get reports
            GetDocumentations();

            // Add False Data for Phase grid view
            AddFalseData();
        }

        void AddFalseData()
        {

        }

        protected void ShowProjectInfo()
        {
            ws.GetProjectDetailCompleted += new EventHandler<GetProjectDetailCompletedEventArgs>(ws_GetProjectDetailCompleted);
            ws.GetProjectDetailAsync(projectID);
        }

        protected void GetDocumentations()
        {
            ws.GetFoldersCompleted += new EventHandler<GetFoldersCompletedEventArgs>(ws_GetFoldersCompleted);
            ws.GetFoldersAsync(projectID);
        }

        void ws_GetFoldersCompleted(object sender, GetFoldersCompletedEventArgs e)
        {
            documentList.Clear();
            List<folder> folderList = new List<folder>();
            List<Documentation> docList = new List<Documentation>();
            folderList = e.Result;

            foreach (folder eachFolder in folderList)
            {
                foreach (documentation eachDoc in eachFolder.documentations)
                {
                    documentList.Add(eachDoc);
                    docList.Add(new Documentation()
                    {
                        ID = eachDoc.documentation_id,
                        Name = eachDoc.documentation_name,
                        Status = eachDoc.documentation_status,
                        Url = eachDoc.documentation_url,
                        UploadDate = eachDoc.documentation_date
                    });
                }
            }

            datagridDocuments.ItemsSource = docList;
        }

        protected void GetCurrentUserInfo()
        {
            ws.GetUserDetailByEmailCompleted += new EventHandler<GetUserDetailByEmailCompletedEventArgs>(ws_GetUserDetailByEmailCompleted);
            ws.GetUserDetailByEmailAsync(userEmail);
        }

        protected void GetMilestone()
        {
            ws.GetMilestonesCompleted += new EventHandler<GetMilestonesCompletedEventArgs>(ws_GetMilestonesCompleted);
            ws.GetMilestonesAsync(projectID);
        }

        protected void GetTeamMember()
        {
            ws.GetProjectMembersCompleted += new EventHandler<GetProjectMembersCompletedEventArgs>(ws_GetProjectMembersCompleted);
            ws.GetProjectMembersAsync(projectID);
        }

        //GET MEMBER LIST - MISSING ROLE
        void ws_GetProjectMembersCompleted(object sender, GetProjectMembersCompletedEventArgs e)
        {
            List<Member> ml = new List<Member>();

            memberList = e.Result;

            for (int i = 0; i < memberList.Count; ++i)
            {
                // Sex
                string[] sexValue = new string[2] { "Female", "Male" };
                string sex;
                if ((int)memberList[i].user_sex == -1)
                    sex = "N/A";
                else
                    sex = sexValue[(int)memberList[i].user_sex];

                ml.Add(new Member(memberList[i].user_id.ToString(), memberList[i].user_email,
                    memberList[i].user_firstname,
                    memberList[i].user_lastname,
                    "Role not functional yet",
                    Convert.ToDateTime(memberList[i].user_birthday),
                    sex,
                    memberList[i].user_address, memberList[i].user_phone));
            }

            //bind

            // Implement datagrid
            PagedCollectionView memberListView = new PagedCollectionView(ml);
            this.datagridMemberList.ItemsSource = memberListView;
            if (memberListView.CanGroup == true)
            {
                // Group tasks by Milestone Name
                memberListView.GroupDescriptions.Add(new PropertyGroupDescription("Role"));
            }
            if (memberListView.CanSort == true)
            {
                // By default, sort by Phase Name.
                memberListView.SortDescriptions.Add(new SortDescription("Email", ListSortDirection.Descending));
            }
        }

        //GET MILESTONE
        void ws_GetMilestonesCompleted(object sender, GetMilestonesCompletedEventArgs e)
        {
            //bind the milestone list to datagrid
            milestoneList = e.Result;
            List<Milestone> ML = new List<Milestone>();
            List<Phase> phaseList = new List<Phase>();

            for (int i = 0; i < milestoneList.Count; ++i)
            {
                ML.Add(new Milestone()
                {
                    ID = milestoneList[i].milestone_id.ToString(),
                    Name = milestoneList[i].milestone_name,
                    // Change here
                    StartDate = milestoneList[i].milestone_start_date,
                    EndDate = milestoneList[i].milestone_end_date,
                    Description = milestoneList[i].milestone_description
                });

                if (milestoneList[i].phases != null)
                {
                    for (int j = 0; j < milestoneList[i].phases.Count; ++j)
                    {
                        phaseList.Add(new Phase()
                        {
                            ID = milestoneList[i].phases[j].phase_id.ToString(),
                            Name = milestoneList[i].phases[j].phase_name,
                            MilestoneID = milestoneList[i].milestone_id.ToString(),
                            Milestone = milestoneList[i].milestone_name,
                            StartDate = milestoneList[i].phases[j].phase_start_date,
                            EndDate = milestoneList[i].phases[j].phase_end_date,
                            Description = milestoneList[i].phases[j].phase_description
                        });
                    }
                }
            }


            //bind Milestone
            this.datagridMilestone.ItemsSource = ML;

            // Implement datagrid
            PagedCollectionView phaseListView = new PagedCollectionView(phaseList);
            this.datagridPhase.ItemsSource = phaseListView;
            if (phaseListView.CanGroup == true)
            {
                // Group tasks by Milestone Name
                phaseListView.GroupDescriptions.Add(new PropertyGroupDescription("Milestone"));
            }
            if (phaseListView.CanSort == true)
            {
                // By default, sort by Phase Name.
                phaseListView.SortDescriptions.Add(new SortDescription("Phase Name", ListSortDirection.Descending));
            }

            //get phase

        }

        void OwnedProject_Loaded(object sender, RoutedEventArgs e)
        {

        }

        void ws_GetUserDetailByEmailCompleted(object sender, GetUserDetailByEmailCompletedEventArgs e)
        {
            currUser = e.Result;
            txtblokEmail.Text = currUser.user_email;
        }



        void ws_GetProjectDetailCompleted(object sender, GetProjectDetailCompletedEventArgs e)
        {
            currentProject = e.Result;

            txtboxProjectName.Text = currentProject.project_name;
            txtblokProjectName.Text = currentProject.project_name;
            txtblokStartDate.Text = currentProject.project_start_date.ToString();
            txtboxDescription.Text = currentProject.project_description;
        }

        // Executes when the user navigates to this page.
        protected override void OnNavigatedTo(NavigationEventArgs e)
        {
        }

        private void buttLogout_Click(object sender, RoutedEventArgs e)
        {
            PageSwitch ps = this.Parent as PageSwitch;
            ps.Navigate(new MainPage());
        }

        private void buttProfile_Click(object sender, RoutedEventArgs e)
        {
            Profile p = new Profile(currUser);
            p.Closed += new EventHandler(pClose);
            p.Show();
        }

        //UPDATE PROFILE
        void pClose(object sender, EventArgs e)
        {
            Profile p = (Profile)sender;
            if (p.DialogResult == true)
            {
                user newUser = new user();

                newUser.user_email = currUser.user_email;

                // Address
                if (p.txtboxAddress.Text == "")
                    newUser.user_address = "N/A";
                else
                    newUser.user_address = p.txtboxAddress.Text;

                // Birthday
                if (p.datepickBirthday.SelectedDate.HasValue == false)
                    newUser.user_birthday = Convert.ToDateTime("1/1/1900");
                else
                    newUser.user_birthday = p.datepickBirthday.SelectedDate;

                // First Name
                if (p.txtboxFirstName.Text == "")
                    newUser.user_address = "N/A";
                else
                    newUser.user_firstname = p.txtboxFirstName.Text;

                // Last Name
                if (p.txtboxLastName.Text == "")
                    newUser.user_address = "N/A";
                else
                    newUser.user_lastname = p.txtboxLastName.Text;

                // Pass
                if (p.passboxOldPassword.Password != "")
                    newUser.user_password = p.passboxNewPassword.Password;
                else
                    newUser.user_password = currUser.user_password;

                // Phone
                if (p.txtboxPhoneNumber.Text == "")
                    newUser.user_address = "N/A";
                else
                    newUser.user_phone = p.txtboxPhoneNumber.Text;

                // Sex
                newUser.user_sex = Convert.ToInt32(p.comboSex.SelectedIndex) - 1;

                //update using webservice
                pmWebServiceClient tido = new pmWebServiceClient();
                tido.UpdateUserDetailsCompleted += new EventHandler<System.ComponentModel.AsyncCompletedEventArgs>(ws_UpdateUserDetailsCompleted);
                tido.UpdateUserDetailsAsync(Convert.ToInt32(currUser.user_id), newUser);
            }
        }

        void ws_UpdateUserDetailsCompleted(object sender, System.ComponentModel.AsyncCompletedEventArgs e)
        {
            MessageBox.Show("User is updated successfully!");
            GetCurrentUserInfo();
        }

        private void buttUserControlPanel_Click(object sender, RoutedEventArgs e)
        {
            PageSwitch ps = this.Parent as PageSwitch;
            ps.Navigate(new UserControlPanel());
        }

        //INVITE MEMBER
        private void buttInvite_Click(object sender, RoutedEventArgs e)
        {
            pmWebServiceClient tido = new pmWebServiceClient();
            tido.GetUsersCompleted += new EventHandler<GetUsersCompletedEventArgs>(ws_GetUsersCompleted);
            tido.GetUsersAsync();
        }

        void ws_GetUsersCompleted(object sender, GetUsersCompletedEventArgs e)
        {
            InviteMember iM = new InviteMember(e.Result);
            iM.Closed += new EventHandler(iMClose);
            iM.Show();
        }

        void iMClose(object sender, EventArgs e)
        {
            InviteMember iM = (InviteMember)sender;

            if (iM.DialogResult == true)
            {
                pmWebServiceClient tido = new pmWebServiceClient();
                tido.InviteUserCompleted += new EventHandler<System.ComponentModel.AsyncCompletedEventArgs>(ws_InviteUserCompleted);
                tido.InviteUserAsync(Convert.ToInt32(((MemberEmail)iM.autoboxEmail.SelectedItem).m_ID),
                    projectID,
                    ((ComboBoxItem)iM.comboRole.SelectedItem).Content.ToString());
            }
        }

        void ws_InviteUserCompleted(object sender, System.ComponentModel.AsyncCompletedEventArgs e)
        {
            MessageBox.Show("User is invited!");
            GetTeamMember();
        }

        //ASSIGN ROLE
        private void buttAssignRole_Click(object sender, RoutedEventArgs e)
        {
            selected = (Member)datagridMemberList.SelectedItem;

            AssignRole ar = new AssignRole(selected);
            ar.Closed += new EventHandler(arClose);
            ar.Show();
        }
        void arClose(object sender, EventArgs e)
        {
            AssignRole ar = (AssignRole)sender;

            if (ar.DialogResult == true)
            {
                pmWebServiceClient tido = new pmWebServiceClient();
                tido.ReAssignMemberRoleCompleted += new EventHandler<System.ComponentModel.AsyncCompletedEventArgs>(ws_ReAssignMemberRoleCompleted);

                string[] roleValue = new string[3] { "Team Lead", "Team Member", "Observer" };
                string role = roleValue[ar.comboRole.SelectedIndex - 1];

                tido.ReAssignMemberRoleAsync(Convert.ToInt32(selected.ID), projectID, role);
            }
        }

        void ws_ReAssignMemberRoleCompleted(object sender, System.ComponentModel.AsyncCompletedEventArgs e)
        {
            MessageBox.Show("Memeber role re-assigned!");
            GetTeamMember();
        }

        //CREATE MILESTONE - begin
        private void buttCreateMilestone_Click(object sender, RoutedEventArgs e)
        {
            CreateMileStone cm = new CreateMileStone();
            cm.Closed += new EventHandler(cmClose);
            cm.Show();
        }
        void cmClose(object sender, EventArgs e)
        {
            CreateMileStone cm = (CreateMileStone)sender;
            if (cm.DialogResult == true)
            {
                //milestone newMS = new milestone();

                //newMS.milestone_name = cm.txtboxMilestoneName.Text;
                //newMS.milestone_description = cm.txtboxDescription.Text;
                //newMS.milestone_start_date = Convert.ToDateTime(cm.datepickStartDate.SelectedDate);
                //newMS.project_id = projectID;

                // Description
                string description;
                if (cm.txtboxDescription.Text == "")
                    description = "N/A";
                else
                    description = cm.txtboxDescription.Text;

                // Start Date
                DateTime startDate;
                if (cm.datepickStartDate.SelectedDate.HasValue == false || cm.datepickStartDate.SelectedDate < DateTime.Now)
                    startDate = DateTime.Now.Date;
                else
                    startDate = Convert.ToDateTime(cm.datepickStartDate.SelectedDate);

                // End Date
                DateTime endDate;
                if (cm.datepickEndDate.SelectedDate.HasValue == false || cm.datepickStartDate.SelectedDate < startDate)
                    endDate = startDate;
                else
                    endDate = Convert.ToDateTime(cm.datepickEndDate.SelectedDate);

                pmWebServiceClient tido = new pmWebServiceClient();
                tido.InsertMileStoneCompleted += new EventHandler<System.ComponentModel.AsyncCompletedEventArgs>(ws_InsertMileStoneCompleted);
                tido.InsertMileStoneAsync(projectID, cm.txtboxMilestoneName.Text, description, startDate, endDate);
            }
        }

        void ws_InsertMileStoneCompleted(object sender, System.ComponentModel.AsyncCompletedEventArgs e)
        {
            MessageBox.Show("Milestone is created successfully!");
            GetMilestone();
        }
        //CREATE MILESTONE - END

        //CREATE PHASE - BEGIN
        private void buttCreatePhase_Click(object sender, RoutedEventArgs e)
        {
            CreatePhase cp = new CreatePhase(milestoneList);
            cp.Closed += new EventHandler(cpClose);
            cp.Show();
        }
        void cpClose(object sender, EventArgs e)
        {
            CreatePhase cp = (CreatePhase)sender;
            if (cp.DialogResult == true)
            {
                //phase newPhase = new phase();

                //newPhase.phase_name = cp.txtboxPhaseName.Text;
                //newPhase.phase_description = cp.txtboxDescription.Text;
                //newPhase.phase_start_date = Convert.ToDateTime(cp.datepickStartDate.SelectedDate);
                //newPhase.milestone_id = Convert.ToInt32(((Milestone)cp.autoboxMilestone.SelectedItem).ID);

                // Description
                string description;
                if (cp.txtboxDescription.Text == "")
                    description = "N/A";
                else
                    description = cp.txtboxDescription.Text;

                // Start Date
                DateTime startDate;
                if (cp.datepickStartDate.SelectedDate.HasValue == false || cp.datepickStartDate.SelectedDate < DateTime.Now)
                    startDate = DateTime.Now.Date;
                else
                    startDate = Convert.ToDateTime(cp.datepickStartDate.SelectedDate);

                // End Date
                DateTime endDate;
                if (cp.datepickStartDate.SelectedDate.HasValue == false || cp.datepickStartDate.SelectedDate < startDate)
                    endDate = startDate;
                else
                    endDate = Convert.ToDateTime(cp.datepickEndDate.SelectedDate);

                ////webservice command here to insert new phase
                pmWebServiceClient tido = new pmWebServiceClient();
                tido.InsertPhaseCompleted += new EventHandler<System.ComponentModel.AsyncCompletedEventArgs>(ws_InsertPhaseCompleted);
                tido.InsertPhaseAsync(Convert.ToInt32(((Milestone)cp.autoboxMilestone.SelectedItem).ID), cp.txtboxPhaseName.Text, description, startDate, endDate);
            }
        }

        void ws_InsertPhaseCompleted(object sender, System.ComponentModel.AsyncCompletedEventArgs e)
        {
            MessageBox.Show("Phase is created successfully!");
            GetMilestone(); //name is get milestone but actually update milestone and phase
        }

        //CREATE PHASE-END

        // UPLOAD
        private string FileName;
        private void buttUploadFile_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                UploadManager uploader = new UploadManager();
                FileName = uploader.StartSelectFileAndUpload(projectID);

                anotherWebServiceClient aws = new anotherWebServiceClient();
                aws.InsertFolderCompleted += new EventHandler<AsyncCompletedEventArgs>(ws_InsertFolderCompleted);
                aws.InsertFolderAsync(projectID);
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "Error!", MessageBoxButton.OK);
            }

        }

        void ws_InsertFolderCompleted(object sender, AsyncCompletedEventArgs e)
        {
            anotherWebServiceClient aws = new anotherWebServiceClient();
            aws.InsertDocumentationCompleted += new EventHandler<AsyncCompletedEventArgs>(aws_InsertDocumentationCompleted);
            aws.InsertDocumentationAsync(currUser.user_id, projectID, FileName, "Uploaded/" + projectID.ToString() + "/" + FileName, DateTime.Today, "active");
        }

        void aws_InsertDocumentationCompleted(object sender, AsyncCompletedEventArgs e)
        {
            MessageBox.Show("File uploaded successfully", "Uploaded", MessageBoxButton.OK);
            GetDocumentations();
        }
        // END UPLOAD

        //EDIT MILESTONE
        private void buttEditMilestone_Click(object sender, RoutedEventArgs e)
        {
            if (datagridMilestone.SelectedIndex >= 0)
            {
                selected_milestone = (Milestone)datagridMilestone.SelectedItem;

                EditMilestone em = new EditMilestone(selected_milestone);
                em.Closed += new EventHandler(em_Closed);
                em.Show();
            }
            else
            {
                MessageBox.Show("Please choose a milestone to edit!");
            }
        }

        void em_Closed(object sender, EventArgs e)
        {
            EditMilestone em = (EditMilestone)sender;
            if (em.DialogResult == true)
            {
                milestone updatedMS = new milestone();
                updatedMS.milestone_id = Convert.ToInt32(selected_milestone.ID);


                updatedMS.milestone_name = em.txtboxMilestoneName.Text;

                // Description
                if (em.txtboxDescription.Text == "")
                    updatedMS.milestone_description = "N/A";
                else
                    updatedMS.milestone_description = em.txtboxDescription.Text;

                // Start Date
                updatedMS.milestone_start_date = Convert.ToDateTime(em.datepickStartDate.SelectedDate);

                // End Date
                updatedMS.milestone_end_date = Convert.ToDateTime(em.datepickEndDate.SelectedDate);

                updatedMS.project_id = currentProject.project_id;

                pmWebServiceClient tido = new pmWebServiceClient();
                tido.UpdateMilestoneDetailCompleted += new EventHandler<AsyncCompletedEventArgs>(webs_UpdateMilestoneDetailCompleted);
                tido.UpdateMilestoneDetailAsync(Convert.ToInt32(selected_milestone.ID), updatedMS);
            }
        }

        void webs_UpdateMilestoneDetailCompleted(object sender, AsyncCompletedEventArgs e)
        {
            MessageBox.Show("Milestone updated!");
            GetMilestone();
        }


        //EDIT PHASE
        private void buttEditPhase_Click(object sender, RoutedEventArgs e)
        {
            if (datagridPhase.SelectedIndex >= 0)
            {
                selected_phase = (Phase)datagridPhase.SelectedItem;
                EditPhase ep = new EditPhase(selected_phase, milestoneList);
                ep.Closed += new EventHandler(ep_Closed);
                ep.Show();
            }
            else
            {
                MessageBox.Show("Please choose a phase to edit!");
            }
        }

        void ep_Closed(object sender, EventArgs e)
        {
            EditPhase p = (EditPhase)sender;

            if (p.DialogResult == true)
            {
                phase newP = new phase();

                newP.phase_id = Convert.ToInt32(selected_phase.ID);
                newP.milestone_id = Convert.ToInt32(((Milestone)p.autoboxMilestone.SelectedItem).ID);
                newP.phase_description = p.txtboxDescription.Text;
                newP.phase_end_date = Convert.ToDateTime(p.datepickEndDate.SelectedDate);
                newP.phase_name = p.txtboxPhaseName.Text;
                newP.phase_start_date = Convert.ToDateTime(p.datepickStartDate.SelectedDate);

                pmWebServiceClient ws = new pmWebServiceClient();
                ws.UpdatePhaseDetailCompleted += new EventHandler<AsyncCompletedEventArgs>(ws_UpdatePhaseDetailCompleted);
                ws.UpdatePhaseDetailAsync(newP.phase_id, newP);
            }
        }

        void ws_UpdatePhaseDetailCompleted(object sender, AsyncCompletedEventArgs e)
        {
            MessageBox.Show("Phase is updated successfully!");
            GetMilestone();//update milestone & phase list
        }

        //UPDATE PROJECT INFO
        private void buttUpdateInformation_Click(object sender, RoutedEventArgs e)
        {
            project updated = new project();

            updated.project_id = currentProject.project_id;
            updated.project_name = txtboxProjectName.Text;
            updated.project_start_date = currentProject.project_start_date;
            updated.manager_id = currentProject.manager_id;
            updated.project_description = txtboxDescription.Text;
            updated.project_end_date = currentProject.project_end_date;

            pmWebServiceClient tido = new pmWebServiceClient();
            tido.UpdateProjectDetailsCompleted += new EventHandler<System.ComponentModel.AsyncCompletedEventArgs>(ws_UpdateProjectDetailsCompleted);
            tido.UpdateProjectDetailsAsync(currentProject.project_id, updated);
        }

        void ws_UpdateProjectDetailsCompleted(object sender, System.ComponentModel.AsyncCompletedEventArgs e)
        {
            MessageBox.Show("Project Information is updated!");
            ShowProjectInfo();
        }

        private void buttExpand_Click(object sender, RoutedEventArgs e)
        {
            PagedCollectionView pcv = datagridPhase.ItemsSource as PagedCollectionView;
            try
            {
                foreach (CollectionViewGroup group in pcv.Groups)
                {
                    datagridPhase.ExpandRowGroup(group, true);
                }
            }
            catch (Exception ex)
            {
                // Could not expand group.
                MessageBox.Show(ex.Message);
            }

        }

        private void buttCollapse_Click(object sender, RoutedEventArgs e)
        {
            PagedCollectionView pcv = datagridPhase.ItemsSource as PagedCollectionView;
            try
            {
                foreach (CollectionViewGroup group in pcv.Groups)
                {
                    datagridPhase.ScrollIntoView(group, null);
                    datagridPhase.CollapseRowGroup(group, true);
                }
            }
            catch (Exception ex)
            {
                // Could not collapse group.
                MessageBox.Show(ex.Message);
            }
        }

        //DELETE MILESTONE
        private void buttDeleteMilestone_Click(object sender, RoutedEventArgs e)
        {
            if (datagridMilestone.SelectedIndex >= 0)
            {
                //get milestone
                selected_milestone = (Milestone)datagridMilestone.SelectedItem;

                ConfirmWindow cw = new ConfirmWindow(selected_milestone);
                cw.Loaded += new RoutedEventHandler(cw_Loaded);
                cw.Show();
            }
            else
            {
                MessageBox.Show("Please choose a milestone!");
            }
        }

        void cw_Loaded(object sender, RoutedEventArgs e)
        {
            ConfirmWindow cw = (ConfirmWindow)sender;
            if (cw.DialogResult == true)
            {
                pmWebServiceClient tido = new pmWebServiceClient();
                tido.DeleteMilestoneCompleted += new EventHandler<AsyncCompletedEventArgs>(webs_DeleteMilestoneCompleted);
                tido.DeleteMilestoneAsync(Convert.ToInt32(selected_milestone.ID));
            }
        }

        void webs_DeleteMilestoneCompleted(object sender, AsyncCompletedEventArgs e)
        {
            MessageBox.Show("MileStone was deleted successfully!");
            GetMilestone();
        }

        //DELETE PHASE
        private void buttDeletePhase_Click(object sender, RoutedEventArgs e)
        {
            if (datagridPhase.SelectedIndex >= 0)
            {
                selected_phase = (Phase)datagridPhase.SelectedItem;
                ConfirmWindow cwp = new ConfirmWindow(selected_phase);
                cwp.Loaded += new RoutedEventHandler(cwp_Loaded);
                cwp.Show();
            }
            else
            {
                MessageBox.Show("Please choose a phase!");
            }
        }

        void cwp_Loaded(object sender, RoutedEventArgs e)
        {
            ConfirmWindow cwp = (ConfirmWindow)sender;
            if (cwp.DialogResult == true)
            {
                pmWebServiceClient tido = new pmWebServiceClient();
                tido.DeletePhaseCompleted += new EventHandler<AsyncCompletedEventArgs>(webs_DeletePhaseCompleted);
                tido.DeletePhaseAsync(Convert.ToInt32(selected_phase.ID));
            }
        }

        void webs_DeletePhaseCompleted(object sender, AsyncCompletedEventArgs e)
        {
            MessageBox.Show("Phase was deleted successfully!");
            GetMilestone();//update milestone and phase list
        }

        private void buttReject_Click(object sender, RoutedEventArgs e)
        {
            Member sm = new Member();
            sm = (Member)datagridMemberList.SelectedItem;

            pmWebServiceClient tido = new pmWebServiceClient();
            tido.RejectMemberCompleted += new EventHandler<AsyncCompletedEventArgs>(webs_RejectMemberCompleted);
            tido.RejectMemberAsync(Convert.ToInt32(sm.ID), currentProject.project_id);
        }

        void webs_RejectMemberCompleted(object sender, AsyncCompletedEventArgs e)
        {
            MessageBox.Show("Member was rejected successfully!");
            GetTeamMember();
        }

        private void buttDownloadFile_Click(object sender, RoutedEventArgs e)
        {
            Documentation selectedRow = (Documentation)datagridDocuments.SelectedItem;
            string url = System.Windows.Application.Current.Host.Source.AbsoluteUri;
            url = url.Substring(0, url.IndexOf("ClientBin")) + selectedRow.Url;
            UriBuilder ub = new UriBuilder(url);
            HtmlPage.Window.Navigate(ub.Uri);
        }

        private void buttDeleteFile_Click(object sender, RoutedEventArgs e)
        {
            documentation seletedD = (documentation) datagridDocuments.SelectedItem;

            ws.DeleteDocmentationCompleted += new EventHandler<AsyncCompletedEventArgs>(ws_DeleteDocmentationCompleted);
            ws.DeleteDocmentationAsync(seletedD.documentation_id);
        }

        void ws_DeleteDocmentationCompleted(object sender, AsyncCompletedEventArgs e)
        {
            GetDocumentations();
        }

    }
}
